package com.my.tag;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
import java.io.IOException;

/**
 * Created by YJH on 2017/7/5 15:13.
 */
public class HelloSimpleTag implements SimpleTag {

    private PageContext pageContext;
    private String value;
    private String count;

    /**
     * JSP引擎调用，把代表JSP页面的PageContext对象传入
     * PageContext可以获得JSP页面的其他8个隐含对象
     * 所以凡是JSP页面可以做的标签处理器都可以完成
     *
     * @param jspContext JspContext
     */
    @Override
    public void setJspContext(JspContext jspContext) {
        this.pageContext = (PageContext) jspContext;
        println("setJspContext()--->");
    }

    @Override
    public void doTag() throws JspException, IOException {
//        println("doTag() ---→ value = " + value + "，count = " + count);
//
//        pageContext.getOut().println("你好，北京！");
//        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
//        pageContext.getOut().println("name = " + request.getParameter("name"));

        JspWriter out = pageContext.getOut();
        int c = 0;
        c = Integer.parseInt(count);
        for (int i = 0; i < c; i++) {
            out.println(value);
        }
    }

    @Override
    public void setParent(JspTag jspTag) {
        println("setParent()");
    }

    @Override
    public JspTag getParent() {
        println("getParent()");
        return null;
    }

    @Override
    public void setJspBody(JspFragment jspFragment) {
        println("setJspBody()");
    }

    private void println(String content) {
        System.out.println(content);
    }


    public void setValue(String value) {
        this.value = value;
    }

    public void setCount(String count) {
        this.count = count;
    }
}
